<template>
  <div id="message-main">
    <div id="message-top">
        <div id="message-title-line"></div>
        <div id="model-title-text">消息管理</div>
    </div>
    <div id="message-dv">
        <div id="message-left">
            <div :class="[{'message-left-people':true},{'peopleBg':obj.c == true}]" v-for="(obj,index) in list" :key="index" @click="clickFn(obj.name,obj.id)">
                <div class="xx" @click.stop="delList(obj.id)">×</div>
                <div class="userImg">{{ obj.n }}</div>
                <div class="userName">
                    {{ obj.name }}
                    <span class="msg-time"> {{ obj.time }} </span>
                </div>
                <div class="people-message"> {{ obj.msg }} </div>
            </div>
        </div>
        <div id="message-right">
            <div id="message-right-title"> {{ uName }} </div>
            <div id="message-right-center">
                <div class="message-right-text" v-for="(obj,index) in list2" :key="index">
                    <div :class="[{'user-img':obj.type == 1},{'doc-img':obj.type == 2}]">
                        <span v-if="obj.type == 1"> {{ xName }} </span>
                        <span v-if="obj.type == 2"> {{ dName }} </span>
                    </div>
                    <div :class="[{'flex-left':obj.type == 1},{'flex-right':obj.type == 2}]">
                        <span :class="[{'user-msg':obj.type ==1},{'doc-msg':obj.type == 2}]"> {{ obj.msg }} </span>
                    </div>
                </div>
                <br>
                <div class="message-right-text"></div>
            </div>
            <div id="message-right-bottom">
                <textarea id="txt" v-model="docToMsg" @keyup.enter="goMsg" :disabled="txtFlag"></textarea>
                <el-button type="primary" @click="goMsg">发送</el-button>
            </div>
        </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
axios.defaults.baseURL = "http://localhost:8080"
import moment from 'moment'
import ModelTitle from '@/components/ModelTitle.vue'
export default {
  components: { ModelTitle },
    created(){
        if(this.$route.query.id!=null){
            this.listLoad()
            this.uName = this.$route.query.name
        }
        this.list1.forEach(resp =>{
            if(resp.uid == this.$route.query.id){
                this.list2.push(resp)
            }
        })
        this.dName = window.sessionStorage.getItem('doctorName').substring(0,1)
        document.querySelector('body').setAttribute('style', 'background-color:#F2F7FB')
    },
    data () {
        return {
            list:JSON.parse(localStorage.getItem("pList"))||[],
            list1:JSON.parse(localStorage.getItem("pList1"))||[],
            list2:[],
            uName:"",
            docToMsg:"",
            userId:0,
            xName:"",
            dName:"",
            txtFlag:false
        }
    },
     watch: {
        list: {
            handler() {
                localStorage.setItem("pList",JSON.stringify(this.list))
            },
            deep: true
        },
        list1: {
            handler() {
                localStorage.setItem("pList1",JSON.stringify(this.list1))
            },
            deep: true
        }
    },
    methods:{
        clickFn(name,id){
            this.list2 = []
            this.userId = id
            this.uName = name
            this.xName = name.substring(0,1)
            let arr = this.list
            for(let i=0;i<arr.length;i++){
                arr[i].c == false
            }   
            let arr1 = this.list1
            for(let i=0;i<arr1.length;i++){
                if(arr1[i].uid == id){
                    this.list2.push(arr1[i])
                }
            }
        },
        listLoad(){
            this.userId = this.$route.query.id
            axios({
                url:"/sign/getListById",
                method:"get",
                params:{
                    id:this.$route.query.id
                }
            }).then(res =>{
                let count = 0
                this.list.forEach(resp =>{
                    if(resp.id == this.$route.query.id){
                        count++
                    }
                })
                if(count==0){
                    this.list.push({
                        id: res.data.user_id,
                        name: res.data.user_name,
                        time: moment(new Date()).format("HH:mm"),
                        n:res.data.user_name.substring(0,1),
                        msg:"[自动回复] 您好，我现在有事不在，请稍后联系！！"
                    })
                }
                if(this.list.length==0){
                    this.txtFlag = true
                }else{
                    this.txtFlag = false
                }
                this.xName = res.data.user_name.substring(0,1)
            })
        },
        delList(id){
            let index = this.list.findIndex(obj => obj.id === id)
            this.list.splice(index,1)
            this.list2 = []
            this.uName = ""
            if(this.list.length == 0){
                this.txtFlag = true
            }else{
                this.txtFlag = false
            }
        },
        goMsg(){
            axios({
                url:"/res/fasongMsg",
                method:"post",
                params:{
                    id: window.sessionStorage.getItem('userId'),
                    msg: this.docToMsg
                }
            }).then(res =>{
                this.list1.push({
                    type: 2,
                    doc_id: res.data.id,
                    msg: res.data.msg,
                    uid:this.userId
                })
                this.list2.push({
                    type: 2,
                    doc_id: res.data.id,
                    msg: res.data.msg,
                    uid:this.userId
                })
                this.list1.push({
                    type:1,
                    msg:"[自动回复] 您好，我现在有事不在，请稍后联系！！",
                    uid:this.userId
                })
                this.list2.push({
                    type: 1,
                    doc_id: res.data.id,
                    msg: "[自动回复] 您好，我现在有事不在，请稍后联系！！",
                    uid:this.userId
                })
            })
            // this.list1.push({
            //     id:"5",
            //     type:2,
            //     msg:this.docToMsg
            // })
            this.docToMsg = ""
            
        }
    }
}
</script>

<style>
    .peopleBg{
        background: #F2F7FB;
    }
    #message-main{
        width: 1150px;
        height: 900px;
        margin: 20px auto;
        background: white;
    }
    #message-title-line{
        width: 6px;
        height: 21px;
        background: #2984F8;
        border-radius: 10px;
        margin-left: 20px;
        position: relative;
        top: -1px;
    }
    #message-title-text{
        margin-left: 10px;
        font-size: 18px;
    }
    #message-top{
        width: 100%;
        height: 60px;
        border-bottom: 1px solid rgb(224, 224, 224);
        display: flex;
        align-items: center;
    }
    #message-dv{
        width: 100%;
        height: 840px;
        display: flex;
    }
    #message-left{
        width: 250px;
        height: 100%;
        border-right: 1px solid rgb(224, 224, 224);
        overflow: auto;
    }
    #message-right{
        width: 900px;
        height: 100%;
    }
    #message-right-title{
        width: 100%;
        height: 6%;
        line-height: 50px;
        text-indent: 20px;
    }
    #message-right-center{
        width: 100%;
        height: 72%;
        background: #F9F9F9;
        overflow: hidden;
        overflow:auto
    }
    #message-right-bottom{
        width: 100%;
        height: 22%;
        background: white;
        position: relative;
    }
    #message-right-bottom .el-button{
        position: absolute;
        bottom: 20px;
        right: 20px;
    }
    #txt{
        padding: 20px;
        width: 100%;
        height: 80%;
        border: 0;
        resize: none;
    }
    #txt:focus{
        outline: none;
    }
    .message-left-people{
        width: 100%;
        height: 80px;
        border-bottom: 1px solid rgb(224, 224, 224);
        position: relative;
        display: flex;
        transition: 0.3s;
        cursor: pointer;
    }
    .userImg{
        width: 60px;
        height: 60px;
        border-radius: 50%;
        background: #28D094;
        margin-top: 10px;
        margin-left: 20px;
        text-align: center;
        line-height: 60px;
        color: white;
        font-size: 25px;
    }
    .userName{
        width: 100px;
        margin-top: 15px;
        margin-left: 10px;
        position: absolute;
        top: -0px;
        left: 85px;
    }
    .people-message{
        position: absolute;
        bottom: 15px;
        left: 95px;
        font-size: 12px;
        color: darkgray;
    }
    .msg-time{
        color: darkgray;
        font-size: 13px;
        position: absolute;
        left: 110px;
        top: 1px;
    }
    .xx{
        position: absolute;
        font-size: 20px;
        top: 28px;
        left: 3px;
        color: darkgray;
        display: none;
        transition: 0.3s;
    }
    .message-left-people:hover .xx{
        display: block;
    }
    .message-right-text{
        width: 100%;
        margin-top: 10px;
        position: relative;
        height: 80px;
        /* background: rgb(169, 169, 169); */
    }
    .user-img{
        width: 50px;
        height: 50px;
        background: #28D094;
        border-radius: 50%;
        margin-left: 10px;
        line-height: 50px;
        text-align: center;
        font-size: 18px;
        color: white;
        display: inline-block;
        position: relative;
        top: -20px;
    }
    .doc-img{
        width: 50px;
        height: 50px;
        background: #5154e0;
        border-radius: 50%;
        margin-left: 10px;
        position: absolute;
        right: 10px;
        display: inline-block;
        line-height: 50px;
        text-align: center;
        font-size: 18px;
        color: white;
        display: inline-block;
    }
    .user-msg{
        background: white;
        padding: 15px;
        margin-left: 10px;
        display: inline-block;
        margin-top: 20px;
        border-radius: 0 20px 20px 20px;
        box-shadow: 0 0 5px darkgray;
    }
    .doc-msg{
        background: white;
        padding: 15px;
        margin-top: 20px;
        display: inline-block;
        border-radius: 20px 0 20px 20px;
        box-shadow: 0 0 5px darkgray;
        margin-right: 10px;
    }
    .flex-left{
        display: inline-block;
        width: 780px;
        /* background: white; */
    }
    .flex-right{
        display: inline-block;
        width: 840px;
        /* background: white; */
        display: flex;
        justify-content: flex-end;
    }
    /*滚动条整体样式*/
  #message-right-center::-webkit-scrollbar {
   /*高宽分别对应横竖滚动条的尺寸*/
    width: 1px;     
    height: 1px;
  }

  /*滚动条里面小方块*/
  #message-right-center::-webkit-scrollbar-thumb {
    border-radius: 10px;
   -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    background: #2effee;
  }
  
  /*滚动条里面轨道*/
  #message-right-center::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    border-radius: 10px;
    background: #EDEDED;
  }
 
   /*滚动条整体样式*/
   #message-left::-webkit-scrollbar {
   /*高宽分别对应横竖滚动条的尺寸*/
    width: 1px;     
    height: 1px;
  }

  /*滚动条里面小方块*/
   #message-left::-webkit-scrollbar-thumb {
    border-radius: 10px;
   -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    background: #2effee;
  }
  
  /*滚动条里面轨道*/
   #message-left::-webkit-scrollbar-track {
    -webkit-box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
    border-radius: 10px;
    background: #EDEDED;
  }
</style>